﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IPR_StudyMeetingMrgSys.DBUtility;//Please add references
namespace IPR_StudyMeetingMrgSys.DAL
{
	/// <summary>
	/// 数据访问类:AttendanceSet
	/// </summary>
	public partial class AttendanceSet
	{
		public AttendanceSet()
		{}
		#region  BasicMethod



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(IPR_StudyMeetingMrgSys.Model.AttendanceSet model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into AttendanceSet(");
			strSql.Append("SignInTime,SignOutTime,RandomNum,Define1,Define2,Define3,Define4,Define5,StartSignInTime,EndSignOutTime)");
			strSql.Append(" values (");
			strSql.Append("@SignInTime,@SignOutTime,@RandomNum,@Define1,@Define2,@Define3,@Define4,@Define5,@StartSignInTime,@EndSignOutTime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@SignInTime", SqlDbType.Int,4),
					new SqlParameter("@SignOutTime", SqlDbType.Int,4),
					new SqlParameter("@RandomNum", SqlDbType.Int,4),
					new SqlParameter("@Define1", SqlDbType.Int,4),
					new SqlParameter("@Define2", SqlDbType.Int,4),
					new SqlParameter("@Define3", SqlDbType.Int,4),
					new SqlParameter("@Define4", SqlDbType.NVarChar,50),
					new SqlParameter("@Define5", SqlDbType.NVarChar,50),
                    new SqlParameter("@StartSignInTime", SqlDbType.Int,4),
                    new SqlParameter("@EndSignOutTime", SqlDbType.Int,4)};
			parameters[0].Value = model.SignInTime;
			parameters[1].Value = model.SignOutTime;
			parameters[2].Value = model.RandomNum;
			parameters[3].Value = model.Define1;
			parameters[4].Value = model.Define2;
			parameters[5].Value = model.Define3;
			parameters[6].Value = model.Define4;
			parameters[7].Value = model.Define5;
            parameters[8].Value = model.StartSignInTime;
            parameters[9].Value = model.EndSignOutTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(IPR_StudyMeetingMrgSys.Model.AttendanceSet model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update AttendanceSet set ");
            strSql.Append("SignInTime=@SignInTime,");
            strSql.Append("SignOutTime=@SignOutTime,");
            strSql.Append("StartSignInTime=@StartSignInTime,");
            strSql.Append("EndSignOutTime=@EndSignOutTime,");
            strSql.Append("RandomNum=@RandomNum,");
			strSql.Append("Define1=@Define1,");
			strSql.Append("Define2=@Define2,");
			strSql.Append("Define3=@Define3,");
			strSql.Append("Define4=@Define4,");
			strSql.Append("Define5=@Define5");
			strSql.Append(" where AutoID=@AutoID");
			SqlParameter[] parameters = {
					new SqlParameter("@SignInTime", SqlDbType.Int,4),
					new SqlParameter("@SignOutTime", SqlDbType.Int,4),
					new SqlParameter("@RandomNum", SqlDbType.Int,4),
					new SqlParameter("@Define1", SqlDbType.Int,4),
					new SqlParameter("@Define2", SqlDbType.Int,4),
					new SqlParameter("@Define3", SqlDbType.Int,4),
					new SqlParameter("@Define4", SqlDbType.NVarChar,50),
					new SqlParameter("@Define5", SqlDbType.NVarChar,50),
					new SqlParameter("@AutoID", SqlDbType.BigInt,8),
                    new SqlParameter("@StartSignInTime", SqlDbType.Int,4),
                    new SqlParameter("@EndSignOutTime", SqlDbType.Int,4)};
			parameters[0].Value = model.SignInTime;
			parameters[1].Value = model.SignOutTime;
			parameters[2].Value = model.RandomNum;
			parameters[3].Value = model.Define1;
			parameters[4].Value = model.Define2;
			parameters[5].Value = model.Define3;
			parameters[6].Value = model.Define4;
			parameters[7].Value = model.Define5;
            parameters[8].Value = model.AutoID;
            parameters[9].Value = model.StartSignInTime;
            parameters[10].Value = model.EndSignOutTime;

            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long AutoID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from AttendanceSet ");
			strSql.Append(" where AutoID=@AutoID");
			SqlParameter[] parameters = {
					new SqlParameter("@AutoID", SqlDbType.BigInt)
			};
			parameters[0].Value = AutoID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string AutoIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from AttendanceSet ");
			strSql.Append(" where AutoID in ("+AutoIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IPR_StudyMeetingMrgSys.Model.AttendanceSet GetModel(long AutoID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 AutoID,SignInTime,SignOutTime,RandomNum,Define1,Define2,Define3,Define4,Define5,StartSignInTime,EndSignOutTime from AttendanceSet ");
			strSql.Append(" where AutoID=@AutoID");
			SqlParameter[] parameters = {
					new SqlParameter("@AutoID", SqlDbType.BigInt)
			};
			parameters[0].Value = AutoID;

			IPR_StudyMeetingMrgSys.Model.AttendanceSet model=new IPR_StudyMeetingMrgSys.Model.AttendanceSet();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IPR_StudyMeetingMrgSys.Model.AttendanceSet DataRowToModel(DataRow row)
		{
			IPR_StudyMeetingMrgSys.Model.AttendanceSet model=new IPR_StudyMeetingMrgSys.Model.AttendanceSet();
			if (row != null)
			{
				if(row["AutoID"]!=null && row["AutoID"].ToString()!="")
				{
					model.AutoID=long.Parse(row["AutoID"].ToString());
                }
                if (row["SignInTime"] != null && row["SignInTime"].ToString() != "")
                {
                    model.SignInTime = int.Parse(row["SignInTime"].ToString());
                }
                if (row["SignOutTime"] != null && row["SignOutTime"].ToString() != "")
                {
                    model.SignOutTime = int.Parse(row["SignOutTime"].ToString());
                }
                if (row["StartSignInTime"] != null && row["StartSignInTime"].ToString() != "")
                {
                    model.StartSignInTime = int.Parse(row["StartSignInTime"].ToString());
                }
                if (row["EndSignOutTime"] != null && row["EndSignOutTime"].ToString() != "")
                {
                    model.EndSignOutTime = int.Parse(row["EndSignOutTime"].ToString());
                }
                if (row["RandomNum"]!=null && row["RandomNum"].ToString()!="")
				{
					model.RandomNum=int.Parse(row["RandomNum"].ToString());
				}
				if(row["Define1"]!=null && row["Define1"].ToString()!="")
				{
					model.Define1=int.Parse(row["Define1"].ToString());
				}
				if(row["Define2"]!=null && row["Define2"].ToString()!="")
				{
					model.Define2=int.Parse(row["Define2"].ToString());
				}
				if(row["Define3"]!=null && row["Define3"].ToString()!="")
				{
					model.Define3=int.Parse(row["Define3"].ToString());
				}
				if(row["Define4"]!=null)
				{
					model.Define4=row["Define4"].ToString();
				}
				if(row["Define5"]!=null)
				{
					model.Define5=row["Define5"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select AutoID,SignInTime,SignOutTime,RandomNum,Define1,Define2,Define3,Define4,Define5,StartSignInTime,EndSignOutTime ");
			strSql.Append(" FROM AttendanceSet ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" AutoID,SignInTime,SignOutTime,RandomNum,Define1,Define2,Define3,Define4,Define5,StartSignInTime,EndSignOutTime ");
			strSql.Append(" FROM AttendanceSet ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM AttendanceSet ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.AutoID desc");
			}
			strSql.Append(")AS Row, T.*  from AttendanceSet T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "AttendanceSet";
			parameters[1].Value = "AutoID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

